<template>
	<div id="activityDetails" class="activityDetails">
		<div>
			<form-preview :header-label="activityName" :header-value="activityValue" :body-items="list" name="activityDetails">
			</form-preview>

			<flexbox v-if="showButtonUnderPendingConfirm" class="actionBtn">
				<flexbox-item>
					<x-button type="warn" @click.native="onClick('errorResolve')">丢弃</x-button>
				</flexbox-item>
				<flexbox-item>
					<x-button type="primary" @click.native="onClick('completeLesson')">完成</x-button>
				</flexbox-item>
			</flexbox>

			<div v-transfer-dom>
				<confirm v-model="show"
				         :title="title"
				         @on-cancel="onCancel"
				         @on-confirm="onConfirm"
				         @on-show="onShow"
				         @on-hide="onHide">
					<p style="text-align:center;">{{ message }}</p>
				</confirm>
			</div>
		</div>
	</div>
</template>

<script>
	import bus from '@/event/eventBus'
	import NetWorking from '../../../../utils/networking'
	import {AlertModule, FormPreview, XButton, Flexbox, FlexboxItem, Group, Cell, TransferDomDirective as TransferDom, Confirm} from 'vux'
	export default {
		name: "pocustomer-details",
		directives: {
			TransferDom
		},
		components: {
			AlertModule,
			XButton,
			FormPreview,
			Flexbox,
			FlexboxItem,
			Group,
			Cell,
			TransferDom,
			Confirm
		},
		data() {
			return {
				btnClickText: '刷课时',
				btnDisable: false,
				currentMethod: '',
				title: '操作提示',
				show: false,
				message: '',
				showButtonUnderPendingConfirm: false,
				showFeedback: false,
				customerList: [],
				activityModel: '',
				obj: '',
				id: '',
				lesson_id: '',
				activityName: '活动名称',
				activityValue: '',
				activityContent: '活动内容',
				attendees: '',
				list: [{
					label: '类型',
					value: ''
				},{
					label: '时间',
					value: ''
				}, {
					label: '内容',
					value: ''
				}, {
					label: '教师',
					value: ''
				}, {
					label: '学生',
					value: ''
				}],
				editBtn: []
			}
		},
		watch: {
			customerList: function () {
				for(var i=0; i<this.customerList.length; i++){
					//this.isFeedbackDone(this.customerList[i].id, this.id)
				}
			}
		},
		created: function () {
			bus.$emit("barController", 1)
			bus.$emit("setTopSimpleTitle", '活动详情')

			this.activityValue = this.$route.params.title
			this.activityContent = this.$route.params.content

			let _this = this
			NetWorking.doGet(
				'activities/getActivityModel',
				null,
				{
					params: {
						id: this.$route.params.id
					}
				}
			).then(
				response => {
					let rs = response.data
					if(rs.error_code == 0){
						if(rs.activity_model.sysActivity.status == 0 && this.ownedByCurrentUser(rs.activity_model.sysUserList)){
							_this.showButtonUnderPendingConfirm = true
						}
						_this.id = rs.activity_model.sysActivity.id
						_this.activityValue = rs.activity_model.sysActivity.title
						_this.list[0].value = rs.activity_model.type.value
						_this.list[1].value = rs.activity_model.sysActivity.time
						_this.list[2].value = rs.activity_model.sysActivity.content
						_this.list[3].value = _this.userListToString(rs.activity_model.sysUserList)
						_this.list[4].value = _this.customerListToString(rs.activity_model.sysCustomerList)
						_this.lesson_id = rs.activity_model.type.id
					}else{
						alert(rs.error_message);
					}
				}
			)
		},
		methods: {
			onCancel () {
				console.log('on cancel')
			},
			onConfirm (msg) {
				switch(this.currentMethod) {
					case 'errorResolve':
						this.errorResolve()
						break
					case 'completeLesson':
						this.completeLesson()
						break
					default:
						break
				}
			},
			onHide () {
				console.log('on hide')
			},
			onShow () {
				console.log('on show')
			},
			onClick: function(value) {
				switch (value) {
					case 'errorResolve':
						this.currentMethod = 'errorResolve'
						this.message = '确定丢弃吗？'
						this.show = true
						break
					case 'completeLesson':
						this.currentMethod = 'completeLesson'
						this.show = true
						this.message = '确定活动完成了吗？'
						break
					default:
						break
				}
			},
			writeFeedback(customerList, activityModel) {
				this.$router.push({name: "lessonFeedback", params: {customerList, activityModel}});
			},
			ownedByCurrentUser(userList){
				for(var i=0; i<userList.length; i++){
					if(userList[i].username == this.$store.state.user.userModel.sysUser.username){
						return true
					}
				}
				return false
			},
			errorResolve(){
				let _this = this
				NetWorking.doPatch(
					'activities/'+this.id,
					null,
					{
						status: 3
					},
					null
				).then(
					response => {
						let rs = response.data
						if(rs.error_code == 0){
							_this.$router.go(-1)
						}else{
							alert(rs.error_message)
						}
					}
				)
			},
			completeLesson(){
				this.$vux.loading.show({
					text: '处理中...'
				})

				let _this = this
				NetWorking.doPost(
					'activities/completeActivity',
					null,
					null,
					{
						params:{
							id: this.id,
							lesson_id: this.lesson_id,
							lessonType: this.list[0].value,
							time: this.list[1].value,
							teacher: this.list[3].value
						}
					}
				).then(
					response => {
						this.$vux.loading.hide()
						let rs = response.data
						if(rs.error_code == 0){
							_this.showModule()
						}else{
							alert(rs.error_message)
						}
					}
				)
			},
			showModule () {
				let _this = this
				AlertModule.show({
					title: '提示',
					content: '活动成功完成',
					onShow () {
						console.log('Module: I\'m showing')
					},
					onHide () {
						_this.$router.go(-1)
					}
				})
			},
			userListToString(list){
				var str = ''
				for(var i=0; i<list.length; i++){
					str += list[i].nickname + ','
				}
				str = str.substr(0, str.length-1)
				return str
			},
			customerListToString(list){
				var str = ''
				for(var i=0; i<list.length; i++){
					str += list[i].name + ','
				}
				str = str.substr(0, str.length-1)
				return str
			}
		}
	}
</script>

<style>
	.activityDetails {
		margin-top: 46px;
	}
</style>